package cn.itsource.pet.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.pet.pojo.domain.Pet;
import cn.itsource.pet.query.PetQuery;
import cn.itsource.pet.service.IPetService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 宠物控制器
 */
@RestController
@RequestMapping("/pet")
@Api(tags = "宠物管理", description = "宠物操作接口")
public class PetController {

    @Autowired
    private IPetService petService;

    @GetMapping("findOne/{id}")
    public AjaxResult findOne(@PathVariable("id") Long id){

        return AjaxResult.createSuccess(petService.findOne(id));
    }

    /**
     * 查询上架的宠物们
     * @param petQuery
     * @return
     *  /pet/8
     */
    @GetMapping("{id}")
    public AjaxResult upDown(@PathVariable("id") Long id){

         return AjaxResult.createSuccess(petService.findOne(id));
    }

    /**
     * 查询上架的宠物们
     * @param petQuery
     * @return
     */
    @PostMapping("/upDown")
    public AjaxResult upDown(@RequestBody PetQuery petQuery){

        return petService.upDown(petQuery);
    }

    /**
     * 查询上架的宠物们
     * @param petQuery
     * @return
     */
    @PostMapping("/queryHomePage")
    public PageList<Pet> queryPetPage(@RequestBody PetQuery petQuery){

        return petService.queryPetPage(petQuery);
    }

    /**
     * 准备一个宠物发布接口
     */
    @PostMapping("/pageList")
    public PageList<Pet> pageList(@RequestBody PetQuery petQuery){
        //发布
        return petService.findPage(petQuery);
    }

    /**
     * 准备一个宠物发布接口
     */
    @PostMapping("/save")
    public AjaxResult save(@RequestBody Pet pet){
        if (pet.getId() == null){
            petService.save(pet);
        }else{

            //发布
            petService.update(pet);
        }

        return AjaxResult.createSuccess();
    }


}
